package com.example.springfeature.controller;

import com.example.springfeature.bean.SysAppParam;
import com.example.springfeature.bean.TestJsr303DTO;
import com.example.springfeature.init.TestInitializingBean;
import com.example.springfeature.restful.ResultVO;
import com.example.springfeature.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "首页模块")
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private TestInitializingBean testInitializingBean;

    @ApiImplicitParam(name = "name",value = "姓名",required = true)
    @ApiOperation(value = "向客人问好")
    @GetMapping("/sayHi")
    public ResponseEntity<String> sayHi(@RequestParam(value = "name")String name){
        testInitializingBean.testDiyInit();
        return ResponseEntity.ok("Hi:"+name);
    }



    @ApiOperation(value = "test_swagger_jsr303")
    @PutMapping("/testjsr303")
    public ResultVO<String> testjsr303(@RequestBody @Validated TestJsr303DTO req){
        System.err.println(req);
        return ResultVO.ok(req.toString());
    }


    /**
     * 开发中大部分情况，增加、删除、修改、详情都是用一个pojo，
     * 在共用一个pojo的情况下，参数校验需要分组，因为不同业务，可能校验的参数是不同的。
     *
     * 例如新增的时候一般不用校验id有没有，但是在编辑的时候需要校验id为不为空。
     *
     * 这时校验分组就派上用场了
     * @param sysAppParam
     * @return
     */
    @ApiOperation(value = "test_group_jsr303")
    @ApiOperationSupport(includeParameters={"sysAppParam.name",
            "sysAppParam.code","sysAppParam.active"})
    @PostMapping("/testGroup")
    public ResultVO add(@RequestBody @Validated(SysAppParam.add.class)  SysAppParam sysAppParam) {
        System.err.println(sysAppParam);
        return ResultVO.ok(sysAppParam);
    }


    @Autowired
    private UserService userService;

    @GetMapping("/register")
    public String register(String username) {
        userService.register(username);
        return "success";
    }

}